package org.wangle.seckill.service.impl;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.wangle.api.goods.pojo.Goods;
import org.wangle.api.order.provider.OrdersProvider;
import org.wangle.api.properties.MQ.SecKillProperties;
import org.wangle.api.user.pojo.User;
import org.wangle.seckill.service.SecKillService;
import org.wangle.seckill.utils.DoLua;
import org.wangle.seckill.utils.RedisUtils;


@Service
public class SecKillServiceImpl implements SecKillService {

    @Reference
    private OrdersProvider ordersProvider;

    @Override
    public Object secKill(User user, Goods goods) {
        RedisTemplate<String, Object> redisTemplate = RedisUtils.getRedisTemplate();
        Long execute = DoLua.execute(DoLua.goodsScript, new String[]{SecKillProperties.goods_nums_key +goods.getId(),SecKillProperties.goods_buyer_key+goods.getId()}, user.getId().toString());
        if(execute.intValue() == 0){
            redisTemplate.opsForSet().add(SecKillProperties.goods_buyer_key+goods.getId(), user.getId().toString());
        }else if(execute.intValue() == 1){
            return "已经购买过";
        }else {
            return "卖完了";
        }
       return ordersProvider.creatOrder(user, goods);

    }
}
